package com.springboot.demo.web;

import com.springboot.demo.entity.ReaderFine;
import com.springboot.demo.entity.ReturnMsg;
import com.springboot.demo.service.ReaderFineRecordService;
import com.springboot.demo.service.ReaderFineService;
import com.springboot.demo.service.ReturnMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;
//张智嵩
@Controller
public class ReturnMsgController {

    @Autowired
    ReturnMsgService returnMsgService;

    @Autowired
    ReaderFineRecordService readerFineRecordService;

    @Autowired
    private ReaderFineService readerFineService;

    @GetMapping("/returnmsg")
    public String ReturnMsgSolving(Model model){
        List<ReturnMsg> returnMsgs=returnMsgService.getReturnMsg();
        model.addAttribute("remsgs",returnMsgs);
        return "librarian_return";
    }

    @PostMapping("/insertfine/{phone}/{fine}/{time}/{bookid}/{starttime}")
    public String InsertFine(@PathVariable("phone") String phone,@PathVariable("fine") int fine,@PathVariable("time") String time,
        @PathVariable("bookid") long bookid,@PathVariable("starttime") String starttime)
    {

        if (fine > 0) {
            List<ReaderFine> readerFineByPhone = readerFineService.getReaderFine(phone, bookid, starttime);
            for(ReaderFine r : readerFineByPhone) {
                System.out.println("-----------------------"+r);
                readerFineRecordService.insertReaderFineRecord(r.getPhone(), r.getRname(), r.getID(), r.getBname(), r.getStarttime(), r.getEndtime(), r.getFine());
            }
        }
        returnMsgService.updateborrow(phone, bookid, starttime);
        returnMsgService.updatereturn(phone, bookid, starttime);
        //完成归还后，将book表中的这本书的状态设为not borrow
        returnMsgService.Inserfine(phone, fine, time, bookid);
        returnMsgService.returnbook(bookid);
        return "redirect:/returnmsg";
    }

}
